<?php
namespace wap\controllers;

use Yii;
use common\helps\api;

/**
 * Site controller
 */

class ProductController extends CommonController
{
    //商品列表
    public function actionIndex()
    {
        /*$data = api::post('comm/get-category', null);
        $bigCategory = $midCategory = $smallCategory = array();
        if ($data['code'] == 200) {
            $bigCategory = $data['data']['bigCategory'];
            $midCategory = $data['data']['midCategory'];
            $smallCategory = $data['data']['smallCategory'];
        }
        return $this->render('index', [
            'bigCategory' => $bigCategory,
            'midCategory' => $midCategory,
            'smallCategory' => $smallCategory
        ]);*/


        $user = $this->getLoginUser();
        $warehouse = api::post('comm/get-warehouses', $user['shop']);

        return $this->render('index', [
            'warehouse' => $warehouse['data'],
        ]);

//        $warehouse = api::post('comm/get-warehouse', null);
//        return $this->render('index', [
//            'warehouse' => $warehouse['data'],
//        ]);
    }

    //ajax商品查询
    public function actionGetProduct()
    {
        $user = $this->getCache('USER');
        $data = Yii::$app->request->post();

        if (isset($data['warehouseId']) && !empty($data['warehouseId'])) {
            $param = array(
                'userId' => $user['userId'],
                'shopId' => $user['shopId'],
                'warehouseId' => $data['warehouseId'],
//            'bigCategory' => $data['bigCategory'],
//            'midCategory' => $data['midCategory'],
//            'smallCategory' => $data['smallCategory'],

                'page' => isset($data['page']) ? $data['page'] : 0,
            );
            $result = api::post('materiel/get-materiel', $param);


            $retData = array();
            if ($result['code'] == 200) {
                $retData = array();
                $curDate = date('Y-m-d H:i:s');
                foreach ($result['data'] as $ma) {

                    if ($ma['startTime'] <= $curDate && $ma['endTime'] >= $curDate) {
                        $retData[] = $ma;
                    }
                }
            }

            $this->retJson('', 200, ['shopType' => $user['shopType'], 'materiels' => $retData]);

        }

        $this->retJson('', 200, array());
    }
}
